package com.woniu.smart.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.smart.dto.CarDTO;
import com.woniu.smart.dto.VisitorCarDTO;
import com.woniu.smart.entity.Access;
import com.woniu.smart.entity.Car;
import com.woniu.smart.entity.Visitor;
import com.woniu.smart.mapper.AccessMapper;
import com.woniu.smart.mapper.CarMapper;
import com.woniu.smart.mapper.VisitorMapper;
import com.woniu.smart.service.CarService;
import com.woniu.smart.vo.CarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：zhulongxing
 * @date ：Created in 2022/10/20 17:53
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private VisitorMapper visitorMapper;
    @Autowired
    private AccessMapper accessMapper;

    @Override
    public int deleteByIds(List<Integer> visitorIds) {
        return carMapper.deleteByIds(visitorIds);
    }

    @Override
    public List<CarVO> selectAll(CarDTO carDTO) {
        return carMapper.selectAll(carDTO);
    }

    @Override
    public List<CarVO> selectAllTwo(CarDTO carDTO) {
        PageHelper.startPage(carDTO.getPageNow(),carDTO.getPageSize(),true);
        return carMapper.selectAllTwo(carDTO);
    }

    @Override
    public List<CarVO> selectDownExcel(CarDTO carDTO) {
        return carMapper.selectDownExcel(carDTO);
    }

    @Override
    public int deleteByPrimaryKey(Long visitorId) {
        return carMapper.deleteByPrimaryKey(visitorId);
    }

    @Override
    @Transactional
    public int insert(Car car) {
      return carMapper.insert(car);
    }

    @Override
    public int insertSelective(Car car) {
        return carMapper.insertSelective(car);
    }

    @Override
    @Transactional
    public int insertSelectiveTwo(VisitorCarDTO visitorCarDTO) {
        int insert = carMapper.insertSelectiveTwo(visitorCarDTO);
        Visitor visitor = visitorMapper.selectByPrimaryKey(visitorCarDTO.getVisitorId());
        visitor.setCarId(visitorCarDTO.getCarId());
        int update = visitorMapper.updateByPrimaryKeySelective(visitor);
        Access access = accessMapper.selectByPrimaryKey(visitorCarDTO.getVisitorId());
        access.setCarId(visitorCarDTO.getCarId());
        int i = accessMapper.updateByPrimaryKeySelective(access);
        return insert;
    }

    @Override
    public Car selectByPrimaryKey(Long id) {
        return carMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(VisitorCarDTO visitorCarDTO) {
        Car car = new Car();
        car.setVisitorId(visitorCarDTO.getVisitorId());
        carMapper.updateByPrimaryKeySelective(car);
        return 1;
    }

    @Override
    public int updateByPrimaryKey(Car record) {
        return carMapper.updateByPrimaryKey(record);
    }
}
